use rbatis::{crud, impl_select, impl_select_page, rbdc::DateTime, PageRequest};
use serde::{Deserialize, Serialize};
#[derive(Debug, PartialEq, Clone, Default, Deserialize, Serialize)]
pub struct SystemAdmin {
    pub id: Option<u32>,
    pub nickname: Option<String>,
    pub avatar: Option<String>,
    pub account: Option<String>,
    pub password: Option<String>,
    pub salt: Option<String>,
    pub mobile: Option<String>,
    pub is_disable: Option<u8>,
    pub is_delete: Option<u8>,
    pub create_time: Option<DateTime>,
    pub update_time: Option<DateTime>,
    pub delete_time: Option<DateTime>,
}

crud!(SystemAdmin {});

impl_select!(SystemAdmin{select_one_by_id(id: u32) => "`where id = #{id} limit 1`"});

impl_select!(SystemAdmin{select_account_one_by_name(account: &str) => "`where account = #{account} limit 1`"});

impl_select_page!(SystemAdmin{select_page(nickname:&str,account:&str)=>
    "`where is_delete = 0`
    if nickname != '':
      ` and nickname like #{'%'+nickname+'%'}`
    if account != '':
      ` and account like #{'%'+account+'%'}`
    if !sql.contains('count'):
     ` order by create_time desc`"});

/// DTO 对象 - 由插件生成。不应该直接更改。
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
pub struct SystemAdminDto {
    pub id: Option<u32>,
    pub nickname: Option<String>,
    pub avatar: Option<String>,
    pub account: Option<String>,
    pub mobile: Option<String>,
    pub is_disable: Option<u8>,
}
impl From<SystemAdmin> for SystemAdminDto {
    fn from(model: SystemAdmin) -> Self {
        SystemAdminDto {
            id: model.id,
            nickname: model.nickname,
            avatar: model.avatar,
            account: model.account,
            mobile: model.mobile,
            is_disable: model.is_disable,
        }
    }
}

impl From<SystemAdminDto> for SystemAdmin {
    fn from(dto: SystemAdminDto) -> Self {
        SystemAdmin {
            id: dto.id,
            nickname: dto.nickname,
            avatar: dto.avatar,
            account: dto.account,
            password: None,
            salt: None,
            mobile: dto.mobile,
            is_disable: dto.is_disable,
            is_delete: None,
            create_time: None,
            update_time: None,
            delete_time: None,
        }
    }
}

#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
pub struct SystemAdminVo {
    pub id: Option<u32>,
    pub nickname: Option<String>,
    pub avatar: Option<String>,
    pub account: Option<String>,
    pub mobile: Option<String>,
    pub is_disable: Option<u8>,
    pub create_time: Option<String>,
    pub update_time: Option<String>,
    pub delete_time: Option<String>,
}

impl From<SystemAdmin> for SystemAdminVo {
    fn from(vo: SystemAdmin) -> Self {
        SystemAdminVo {
            id: vo.id,
            nickname: vo.nickname,
            avatar: vo.avatar,
            account: vo.account,
            mobile: vo.mobile,
            is_disable: vo.is_disable,
            create_time: vo.create_time.map(|v| v.display_stand()),
            update_time: vo.update_time.map(|v| v.display_stand()),
            delete_time: vo.delete_time.map(|v| v.display_stand()),
        }
    }
}

#[derive(Serialize, Deserialize, Clone, Debug)]
pub struct SystemAdminPageDTO {
    pub page_no: Option<u64>,
    pub page_size: Option<u64>,
    pub account: Option<String>,
    pub nickname: Option<String>,
}

impl From<&SystemAdminPageDTO> for PageRequest {
    fn from(arg: &SystemAdminPageDTO) -> Self {
        PageRequest::new(arg.page_no.unwrap_or(1), arg.page_size.unwrap_or(10))
    }
}
